package pages;

import org.openqa.selenium.*;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import selenium.WebDriverWrapper;
import utils.LogTest;


/**
 * Created by Olga on 22.05.2014.
 */
public class HomePage {
    private final String URL = "http://rozetka.com.ua/";
    public static WebDriverWrapper driver;
    protected final By searchString = By.className("header-search-input-text");
    protected final By inCompare = By.name("comparison_catalog");
    protected final String productFindPatternLocate = "//a[contains(text(),'%s')]";
    protected final By compareButton = By.name("tocomparison");
    protected final By deleteX = By.className("delete");

    public HomePage(WebDriverWrapper driver) {
        this.driver = driver;
        PageFactory.initElements(driver, this);
    }

    public void openHomePage() {
        driver.get(URL);
    }


    public void waitAddToCompare() {
        WebDriverWait wait = new WebDriverWait(driver, 20);
        WebElement compare = driver.findElement(compareButton);
        wait.until(ExpectedConditions.elementToBeClickable(compare));
        LogTest.info("you should wait for button Compare");
    }

    public void waitToCompare() {
        WebDriverWait wait = new WebDriverWait(driver, 20);
        WebElement compareCatalog = driver.findElement(inCompare);
        wait.until(ExpectedConditions.elementToBeClickable(compareCatalog));
        LogTest.info("you should wait for button inCompare");
    }

    public void waitForText(String text) {
        WebDriverWait wait = new WebDriverWait(driver, 20);
        wait.until(ExpectedConditions.presenceOfElementLocated(By.partialLinkText(text)));
    }

    public boolean searchGood(String productName) {

        driver.findElement(searchString).sendKeys(productName);
        driver.findElement(searchString).sendKeys(Keys.RETURN);
        try {
            if (driver.findElement(By.xpath(String.format(productFindPatternLocate, productName))).isDisplayed()) {
                LogTest.info(productName + " found");
                return true;
            }
        } catch (org.openqa.selenium.TimeoutException e) {
            LogTest.error(productName + " not found");
        }
        return false;
    }

    public boolean addToCompare() {

        WebElement compare = driver.findElement(compareButton);

        if (compare != null) {
            LogTest.info("Added to comparison");
            compare.click();
            return true;
        } else {
            LogTest.error("Button \"Compare\" wasn't found");
            return false;
        }

    }

    public void clearSearchBox() {
        driver.findElement(searchString).clear();
    }

    public void gotoComparison() {
        driver.findElement(inCompare).click();
    }

    public boolean checkComparison(String productName) {
        try {
            WebElement product = driver.findElement(By.partialLinkText(productName));

            if (product != null) {
                LogTest.info(productName + " in Comparison List");
                return true;
            }
        } catch (TimeoutException e) {
            LogTest.error(productName + " is absend");

        }
        return false;
    }

    public void deleteFromComparisonList(String productName) {
        if (driver.findElement(By.linkText(productName)) != null) {
            driver.findElement(deleteX).click();
            LogTest.info(productName + " deleted");
        } else {
            LogTest.error(" fail ");
        }
    }

    public void gotoHomePage() {
        driver.navigate().to(URL);
    }


}
